package BuyerAgent;

import jade.core.behaviours.OneShotBehaviour;

/**
 * This is the behavior of buyer agent that it moves to the main container
 * before purchasing trees
 * 
 * @author DUY HAN PHAN
 * 
 */
public class MoveToMainContainerBehaviour extends OneShotBehaviour {
	private int exitValue = 0; // if exitValue == 0, the move is not success,
								// try to move again
	private volatile Buyer buyerAgent;

	/**
	 * @param buyerAgent
	 */
	public MoveToMainContainerBehaviour(Buyer buyerAgent) {
		super(buyerAgent);
		this.buyerAgent = buyerAgent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jade.core.behaviours.Behaviour#action()
	 */
	@Override
	public void action() {
		// check if current location is home location
		if (!buyerAgent.here().equals(buyerAgent.homeLocationOfBuyerAgent)) {
			System.out.println("ERROR!!! the agent is not at home location");
			exitValue = 0;
			return;
		}

		System.out.println("Agent is moving to " + buyerAgent.mainContainerLocation);
		buyerAgent.doMove(buyerAgent.mainContainerLocation);

		// while(!buyerAgent.here().equals(buyerAgent.mainContainerLocation)) {
		// //wait until buyer agent successfully moves to main container
		// }

		// The buyer agent is at main container location now
		exitValue = 1; // if exitValue == 1, the move is done, change to next
						// behavior state
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jade.core.behaviours.Behaviour#onEnd()
	 */
	@Override
	public int onEnd() {
		return exitValue;
	}
}
